package com.weixin.service;

import com.agent.entity.Dict;
import com.agent.entity.RepaireInfo;
import com.agent.entity.User;
import com.agent.service.IDictService;
import com.agent.service.IRepaireInfoService;
import com.agent.service.UserService;
import com.agent.spring.websocket.SpringWebSocketHandler;
import com.agent.util.Constants;
import com.weixin.pojo.Text;
import com.weixin.pojo.WXMsg;
import com.weixin.util.WxepConstants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.socket.TextMessage;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
@EnableScheduling
public class ScheduleNofify {

    @Resource(name = "repaireInfoService")
    IRepaireInfoService repaireInfoService;

    @Resource(name = "weixinNotifyService")
    WeixinNotifyService weixinNotifyService;

    @Resource(name = "dictService")
    IDictService dictService;

    @Bean
    public SpringWebSocketHandler infoHandler(){
        return new SpringWebSocketHandler();
    }

    @Autowired
    UserService userService;
    /***
     * 一分钟执行一次  将满七分钟未接单的单子推送坐席提醒
     * 从数据库查询抢单模式、自动派单模式 当前时间之前的七分钟的单子
     */
    // TODO 坐席工作台暂时未配置  先不执行  2019-10-12
//    @Scheduled(cron = "0 */1 * * * ?")
    public void execute(){
        try {
            int count = repaireInfoService.selectExpaireInfo();
            if(count==0){
                return;
            }
            List<RepaireInfo> repaireList = repaireInfoService.selectExpaireInfos();
            StringBuffer currentNames = new StringBuffer();
            for (int i=0;i<repaireList.size();i++){
                RepaireInfo repaire = repaireList.get(i);
                if(!StringUtils.isEmpty(repaire.getCurrentName()) && currentNames.toString().indexOf(repaire.getCurrentName()) > 0) {
                    currentNames.append(repaire.getCurrentName()).append("、");
                }
                if(!StringUtils.isEmpty(currentNames.toString()) && i==repaireList.size()-1) {
                    currentNames.replace(currentNames.length()-1,currentNames.length(),"");
                }
            }
            //  微信端提醒
            WXMsg wxMsg = new WXMsg();
            wxMsg.setAgentid(WxepConstants.CUSTOMER_AGENT);
            wxMsg.setMsgtype("text");
            wxMsg.setTouser(findToUser());
            Text text = new Text();
            Dict dict = dictService.getDict(Constants.CALLER_NOTIFY);
            String content = dict.getDictValue().
                    replace("{count}",count+"")+ (StringUtils.isEmpty(currentNames.toString())?"":"负责工程师:["+currentNames+"]");
            text.setContent(content);
            wxMsg.setText(text);
            weixinNotifyService.sendMsg(wxMsg,WxepConstants.CUSTOMER_SECRET);
            infoHandler().sendMessageToUsersByName(findToUser(),new TextMessage(content));
        } catch (Exception e) {
            log.error("com.weixin.service.ScheduleNofify.execute error |",e);
        }
    }

    private String findToUser(){
        User in = new User();
        in.setRoleId(2);
        List<User> list = userService.findUser(in);
        StringBuffer sb = new StringBuffer();
        for(User u:list){
            sb.append(u.getUserName());
            sb.append("|");
        }
        return sb.substring(0,sb.length()-1);
    }
}
